package cn.stylefeng.guns.modular.ums.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.ums.entity.Member;
import cn.stylefeng.guns.modular.ums.entity.MemberAccount;
import cn.stylefeng.guns.modular.ums.entity.MemberWithdraw;
import cn.stylefeng.guns.modular.ums.mapper.MemberAccountMapper;
import cn.stylefeng.guns.modular.ums.mapper.MemberWithdrawMapper;
import cn.stylefeng.guns.modular.ums.model.params.MemberAccountParam;
import cn.stylefeng.guns.modular.ums.model.params.MemberAccountRecordsParam;
import cn.stylefeng.guns.modular.ums.model.params.MemberWithdrawParam;
import cn.stylefeng.guns.modular.ums.model.result.MemberAccountResult;
import cn.stylefeng.guns.modular.ums.model.result.MemberWithdrawResult;
import cn.stylefeng.guns.modular.ums.service.MemberAccountRecordsService;
import cn.stylefeng.guns.modular.ums.service.MemberAccountService;
import cn.stylefeng.guns.modular.ums.service.MemberService;
import  cn.stylefeng.guns.modular.ums.service.MemberWithdrawService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 会员提现记录 服务实现类
 * </p>
 *
 * @author yzb
 * @since 2022-04-28
 */
@Service
public class MemberWithdrawServiceImpl extends ServiceImpl<MemberWithdrawMapper, MemberWithdraw> implements MemberWithdrawService {

    @Resource
    private MemberAccountMapper accountMapper;
    @Resource
    private MemberAccountRecordsService accountRecordsService;
    @Resource
    private MemberService memberService;

    @Transactional
    @Override
    public void add(MemberWithdrawParam param){
        //判断提现金额是否合理
        MemberAccountResult account=accountMapper.findBySpec(new MemberAccountParam().setMemberId(param.getMemberId()));
        if (account!=null && account.getBalanceWithdraw().compareTo(param.getMoney())>=0){
            //可以提现
            param.setLastBalance(account.getBalanceWithdraw());
            param.setBalance(account.getBalanceWithdraw().subtract(param.getMoney()));
            MemberWithdraw entity = getEntity(param);
            this.save(entity);
            //更改账户余额
            MemberAccount accountParam=new MemberAccount();
            accountParam.setAccountId(account.getAccountId());
            accountParam.setBalanceFrozen(account.getBalanceFrozen().add(param.getMoney()));//冻结余额
            accountParam.setBalanceWithdraw(account.getBalanceWithdraw().subtract(param.getMoney()));
            accountMapper.updateById(accountParam);
//            //添加提现记录
//            MemberAccountRecordsParam recordsParam=new MemberAccountRecordsParam();
//            recordsParam.setType(2);
//            recordsParam.setSign(0);
//            recordsParam.setMoney(param.getMoney());
//            recordsParam.setMemberId(param.getMemberId());
//            recordsParam.setDataId(param.getId());
//            recordsParam.setContent("提现申请");
//            accountRecordsService.add(recordsParam);
        }else{
            throw new RuntimeException("当前可提现余额不足");
        }
    }

    @Override
    public void delete(MemberWithdrawParam param){
        this.removeById(getKey(param));
    }

    @Transactional
    @Override
    public void update(MemberWithdrawParam param){
        MemberWithdraw oldEntity = getOldEntity(param);
        MemberWithdraw newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public void withdrawAuth(MemberWithdrawParam param) {
        MemberWithdraw oldEntity = getOldEntity(param);
        MemberWithdraw newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
        //更改账户余额,冻结扣除，总余额扣除
        MemberAccountResult account=accountMapper.findBySpec(new MemberAccountParam().setMemberId(param.getMemberId()));
        if (param.getStatus()==1){
            MemberAccount accountParam=new MemberAccount();
            accountParam.setAccountId(account.getAccountId());
            accountParam.setBalanceFrozen(account.getBalanceFrozen().subtract(param.getMoney()));//冻结余额
            accountParam.setBalance(account.getBalance().subtract(param.getMoney()));
            accountMapper.updateById(accountParam);
            //添加提现记录
            MemberAccountRecordsParam recordsParam=new MemberAccountRecordsParam();
            recordsParam.setType(2);
            recordsParam.setSign(0);
            recordsParam.setMoney(param.getMoney());
            recordsParam.setMemberId(param.getMemberId());
            recordsParam.setDataId(param.getId());
            recordsParam.setContent("提现审核通过");
            accountRecordsService.add(recordsParam);
        }else{//拒绝
            MemberAccount accountParam=new MemberAccount();
            accountParam.setAccountId(account.getAccountId());
            accountParam.setBalanceFrozen(account.getBalanceFrozen().subtract(param.getMoney()));//冻结余额
            accountParam.setBalanceWithdraw(account.getBalanceWithdraw().add(param.getMoney()));
            accountMapper.updateById(accountParam);
        }
    }

    @Override
    public MemberWithdrawResult findBySpec(MemberWithdrawParam param){
        return null;
    }

    @Override
    public List<MemberWithdrawResult> findListBySpec(MemberWithdrawParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(MemberWithdrawParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo findPageBySpecForAdmin(MemberWithdrawParam param) {
        Page pageContext = getPageContext();
        IPage<MemberWithdrawResult> page = this.baseMapper.customPageListForAdmin(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(MemberWithdrawParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private MemberWithdraw getOldEntity(MemberWithdrawParam param) {
        return this.getById(getKey(param));
    }

    private MemberWithdraw getEntity(MemberWithdrawParam param) {
        MemberWithdraw entity = new MemberWithdraw();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
